<template>
  <div class="item1">
    <div class="breadcrumb" :gutter="24">
      {{$t('system.service_status')}}
      <el-form ref="form" :model="formData" label-width="70px" size="small">
        <el-row :gutter="28">
          <!-- <el-col :xs="3" :sm="1" :md="1" :lg="1" :xl="1">
            <el-button type="primary" size="small" :icon="'iconfont ' + icon" class="change" @click="changePattern"></el-button>
          </el-col>

          <el-col :xs="3" :sm="2" :md="2" :lg="2" :xl="2" style="padding-top:10px">
            <el-form-item label-width="0px">
              <el-button type="primary" size="small" @click="handleSearch">{{ $t("common.query") }}</el-button>
            </el-form-item>
          </el-col> -->
          <div class="btngroup" style="margin-right:100px">
            <el-button type="primary" size="small" @click="handleOper('启动','start')">{{$t('run.start_up')}}</el-button>
            <el-button type="primary" size="small" @click="handleOper('停止','stop')">{{$t('run.stop')}}</el-button>
          </div>
        </el-row>
      </el-form>
    </div>

    <!-- 卡片区域 -->
    <div>
      <div class="cardBox" :key="keyTime" style="overflow-x: hidden;" :style="{height:height+'px'} " v-loading="tableLoading">
        <el-row :gutter="15">
          <template v-for="(item,index) in tableData">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="4">
              <div class="card" :key="index" :class="[index == cardIndex ? 'selectCard' : '',getColor(item)]"
                @click="handleIndex(index,item)">
                <p class="time" style="font-size:16px;font-weight:bold;white-space: nowrap;">{{$t('system.service_name')}}：{{item.servName}}
                </p>
                <div class="time">
                  <!-- <el-tooltip :content="item.assetNum" effect="dark" placement="top" v-if="item.assetNum">
                    <p class="Desc">设备编号：{{item.assetNum}}</p>
                  </el-tooltip> -->
                  <p class="desc">{{$t('common.describe')}}：{{item.desc}}</p>
                  <p class="desc">{{$t('common.state')}}：{{item.state=='运行中'? $t('third.in_doing'):item.state}}</p>
                </div>

              </div>
            </el-col>
          </template>
          <el-empty description="暂无数据" v-if="tableData.length == 0"></el-empty>
        </el-row>

        <el-divider></el-divider>

        <el-row :gutter="25">
          <template>
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="4">
              <div class="card" :key="index" :class="[index == cardIndex ? 'selectCard' : '',getPlcColor(tableData[0].plcConn)]">
                <p class="time" style="font-size:18px;font-weight:bold;">{{$t('system.service_name')}}：{{$t('system.communication_status')}}
                </p>
                <div class="time">
                  <p class="desc">{{$t('common.describe')}}：与PLC通讯状态</p>
                  <p class="desc">{{$t('common.state')}}：{{tableData[0].plcConn==1?'已连接':'未连接'}}</p>
                  <!-- <el-tooltip :content="item.assetNum" effect="dark" placement="top" v-if="item.assetNum">
                    <p class="Desc">设备编号：{{item.assetNum}}</p>
                  </el-tooltip> -->
                  <!-- <p class="desc">服务描述：{{item.desc}}</p>
                  <p class="desc">服务状态：{{item.state}}</p> -->
                </div>

              </div>
            </el-col>
          </template>

        </el-row>
      </div>
    </div>

  </div>
</template>

<script>
import { configure } from "@/api/configure"
export default {
  data () {
    return {
      screenWidth: document.body.clientWidth,
      height: document.body.offsetHeight - 230,
      tableData: [],
      formData: {
        keyword: "",
      },
      icon: "icon-biaoge",
      pager: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
        field: 'classId',
        order: '',
      },
      radioId: "",
      servName: '',
      cardIndex: -1,
      isCard: true,
      tableLoading: false,
      keyTime: 0,
      timer: null,
    }
  },
  created () {
    this.getServList()
  },
  destroyed () {
    clearInterval(this.timer)
  },
  methods: {
    handleSearch () {
      this.pager.pageNum = 1;
      this.getServList()
    },
    getServList () {
      const param = {
      }
      configure.getServList(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          this.tableData = res.data
          this.keyTime = new Date().getTime()

          clearInterval(this.timer)
          this.timer = setInterval(() => {
            this.getServList()
          }, 4000)
        }
      }).catch(e => {
        this.tableLoading = false
        this.$message.error('服务器繁忙,请稍后再试');
      })
    },
    sortChange (column) {
      if (column.order === 'descending') {
        this.pager.field = column.prop
        this.pager.order = 'DESC'
      } else {
        this.pager.field = column.prop
        this.pager.order = 'ASC'
      }
      this.pager.pageNum = 1
      this.getServList()
    },
    handleIndex (index, row) {
      this.cardIndex = index
      this.radioId = row.fid;
      this.servName = row.servName
    },
    handleSizeChange (val) {
      this.pager.pageNum = 1
      this.pager.pageSize = val
      this.getServList()
    },
    handleCurrentChange (val) {
      this.pager.pageNum = val;
      this.getServList()
    },
    rowClick (row) {
      this.radioId = row.fid;
      this.$refs.refTable.setCurrentRow(row);
    },
    changePattern () {
      this.isCard = !this.isCard
      this.radioId = ''
      this.cardIndex = -1
      this.icon = this.isCard ? 'icon-qiehuan' : 'icon-biaoge'
    },
    async handleOper (name, cmd) {
      if (this.radioId === '') {
        this.$message.error('请选择服务');
        return
      }
      let that = this
      try {
        that.$customConfirm({
          content: "请确认是否" + name + that.servName + "服务",
          onok () {
            configure.operServ(that.servName, cmd).then(res => {
            }).catch(e => {
              that.loading = false
            })

          }
        });

      } catch (e) { }
    },
    table_index (index) {
      return (this.pager.pageNum - 1) * this.pager.pageSize + index
    },
    getColor (type) {
      if (type.state == '运行中') {
        return 'lv'
      } else if (type.state == '已停止') {
        return 'hong'
      } else if (type.state == '未知') {
        return 'huang'
      } else {
        return 'hui'
      }

    },

    getPlcColor (type) {
      if (type == 1) {
        return 'lv'
      } else if (type == 0) {
        return 'hong'
      }

    },
    statusStyle ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 5) {
        if (this.tableData[rowIndex].tagAddr == null) {
          return 'background:#FFFF00'
        }
      } if (columnIndex == 4) {
        if (this.tableData[rowIndex].tagAddr == null) {
          return 'background:#0e0d0d'
        } else if (this.tableData[rowIndex].devState == 10) {
          return 'background:#22b14c'
        } else if (this.tableData[rowIndex].devState == 21) {
          return 'background:#ed1c24'
        } else if (this.tableData[rowIndex].devState == 11) {
          return 'background:#ffd76a'
        } else if (this.tableData[rowIndex].devState == 51 || this.tableData[rowIndex].devState == 81) {
          return 'background:#b84a5b'
        } else {
          return 'background:#c7c2c8'
        }
      }
    },
    queryStatus (status, tagAddr) {
      if (tagAddr == null) return ''
      switch (status) {
        case 10:
          return this.$t('workorder.function')
        case 11:
          return this.$t('workorder.finish')
        case 21:
          return this.$t('workorder.suspend')
        case 51:
        case 81:
          return "未连接"
        default:
          return "空闲"
      }
    },

  }
}
</script>

<style scoped>
.btngroup {
  float: right;
}
.change >>> .iconfont {
  font-size: 13px !important;
}
.titmepicker >>> .el-date-editor {
  width: 100%;
}
.titmepicker >>> .el-date-editor .el-range-separator {
  width: 8%;
}
.isShowDialog >>> .el-select {
  width: 100%;
}
.tableAuto,
.cardBox {
  clear: both;
}
.pagination-area {
  padding-top: 10px;
}
.Desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cardBox {
  overflow-y: auto;
}
.card {
  background: #eaebec;
  margin-bottom: 20px;
  padding: 10px;
  border-radius: 3px;
}
.card p {
  font-size: 16px;
  line-height: 32px;
  transform: scale(0.9);
}
.desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 20px;
}
.title {
  font-weight: bold;
}
.selectCard {
  background: #f6fbff;
}
@media only screen and (min-width: 1200px) {
  .el-col-lg-5 {
    width: 19.83333%;
  }
}
.lv {
  border-left: 8px solid #22b14c;
}
.hui {
  border-left: 8px solid #c7c2c8;
}
.hong {
  border-left: 8px solid #ed1c24;
}
.huang {
  border-left: 8px solid #ffd76a;
}
.pinhong {
  border-left: 8px solid #b84a5b;
}
.hei {
  border-left: 8px solid #0e0d0d;
}
</style>